package cn.com.dycc.notice.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baidu.yun.push.exception.PushClientException;
import com.baidu.yun.push.exception.PushServerException;

import cn.com.dycc.common.controller.BaseController;
import cn.com.dycc.notice.entity.Message;
import cn.com.dycc.notice.entity.MessageParam;
import cn.com.dycc.notice.service.MessageNotFoundException;
import cn.com.dycc.notice.service.MessageService;
import cn.com.dycc.sys_manage.usersysmanagement.service.UserNotFoundException;
import cn.com.dycc.util.JsonObjectResult;


/**
 * 
 * Description: 系统公告发布控制层
 * Author: Liket
 * Version: 1.0
 * Create Date Time: 2017年8月4日 下午2:21:03.
 * Update Date Time: 2017年8月20日 上午10:27:10.
 * @see
 */
@Controller
@RequestMapping("/message")
public class MessageController extends BaseController{
	@Resource
	private MessageService messageService;
	/**
	 * 添加请求
	 * @param message 用户提交的部分公告参数
	 * @param req 请求参数
	 * @return 返回添加条数
	 * @throws IOException 
	 * @throws PushServerException 
	 * @throws PushClientException 
	 * @throws MessageNotFoundException 
	 * @throws UserNotFoundException 
	 */
	@RequestMapping("/add.do")
	@ResponseBody
	public JsonObjectResult addMessage(@RequestBody MessageParam message,HttpServletRequest req) throws UserNotFoundException, MessageNotFoundException, PushClientException, PushServerException, IOException{
		System.out.println("message="+message);		
		int n = messageService.addMessage(message,req);	
		return new JsonObjectResult(n);
	}
	/**
	 * 阅读请求
	 * @param user_id 用户ID
	 * @param message_id 公告ID
	 * @param replay 回复
	 * @return 返回一个完整实体
	 */
	@RequestMapping("/read.do")
	@ResponseBody
	public JsonObjectResult readMessage(Integer message_id,String replay,HttpServletRequest req){
		//HttpSession session = req.getSession();
		//Integer user_id = (Integer) session.getAttribute("id");
		//MessageReceivePerson mrp = messageService.readMessage(user_id, message_id, replay);
		
		//return new JsonObjectResult(mrp);
		return new JsonObjectResult(0);
	}
	
	/**
	 * 20180510更新  lin fang an
	 * @param req
	 * @param page
	 * @param limit
	 * @param title
	 * @return
	 */
	@RequestMapping("/list.do")
	@ResponseBody	
	public JsonObjectResult listMessage(HttpServletRequest req,Integer page,Integer limit,String title){
		
		List<Message> list = messageService.listMessage(req, page, limit, title);
		String count = messageService.findMessAgeCount(req, title);
//		System.out.println("clist="+list);
//		System.out.println(new JsonObjectResult(list).getData());
		return new JsonObjectResult(count,list);
	}
	
	//铺抓用户信息异常
	
	
	//铺抓公告信息异常
	@ExceptionHandler(MessageNotFoundException.class)
	@ResponseBody
	public JsonObjectResult messageExp(MessageNotFoundException e){
		e.printStackTrace();
		return new JsonObjectResult(9,e);
	}
	
	
	@ExceptionHandler(UnsupportedEncodingException.class)
	@ResponseBody
	public JsonObjectResult unEncodingExp(UnsupportedEncodingException e){
		e.printStackTrace();		
		return new JsonObjectResult(13,e);
	}

	
	/**
	 * 删除请求
	 * @param message 实体里包含参数：message_id 和 top_dept_no
	 * @return 返回删除条数
	 */
	/*@RequestMapping("/delete.do")
	@ResponseBody
	public JsonObjectResult deleteMessage(Message message,HttpServletRequest req){
		//HttpSession session = req.getSession();
		//Integer top_dept_no = (Integer) session.getAttribute("top_dept_no");
		//message.setTop_dept_no(top_dept_no);
		int n = messageService.deleteMessage(message);
		return new JsonObjectResult(n);
	}*/
	
	@RequestMapping("/update.do")
	@ResponseBody
	public JsonObjectResult updateMessage(@RequestBody MessageParam message,HttpServletRequest req) throws MessageNotFoundException, UnsupportedEncodingException{
		
		boolean b = messageService.updateMessage(message,req);
		return new JsonObjectResult(b);
	}
	
	/*@RequestMapping("/page.do")
	@ResponseBody	
	public JsonObjectResult listMessageByPage(Integer page,HttpServletRequest req) throws MessageNotFoundException, IOException{
		
		List<Message> list = messageService.listMessageByPage(page,req);
		return new JsonObjectResult(list);
	}*/
	
	@RequestMapping("/find.do")
	@ResponseBody
	public JsonObjectResult findMessage(String message_id,HttpServletRequest req){
		List<Message> list = messageService.findMessageById(message_id,req);
		
		return new JsonObjectResult(list);
	}
	
	@RequestMapping("/count.do")
	@ResponseBody
	public JsonObjectResult countMessage(HttpServletRequest req){
		int count = messageService.countMessage(req);
		return new JsonObjectResult(count);
	}
	
	@RequestMapping("/deleteall.do")
	@ResponseBody
	public JsonObjectResult deleteMessageAll(String []message_ids,HttpServletRequest req){
		int count = messageService.deleteMessageAll(message_ids, req);
		return new JsonObjectResult(count);
	}
	
	@RequestMapping("/param.do")
	@ResponseBody
	public JsonObjectResult listMessageByParam(HttpServletRequest req,String send_date_start,String send_date_end,String title,String username) throws ParseException{
//		System.out.println("send_date="+send_date_start);
//		System.out.println("send_date2="+send_date_end);
		List<Message> list = messageService.listMessageByParam(req,send_date_start,send_date_end,title,username);
		return new JsonObjectResult(list);
		
	}
		
}
